package main.java.indi.zyj.sword;

import main.java.indi.zyj.hot100.base.TreeNode;

public class IsBalanced {


    public boolean isBalanced(TreeNode root) {

        if (root == null) {
            return true;
        }

        if (Math.abs(getHigh(root.left) - getHigh(root.right)) <= 1) {
            return isBalanced(root.left) && isBalanced(root.right);
        }

        return false;

    }

    private int getHigh(TreeNode root) {
        return root == null ? 0 : Math.max(getHigh(root.left), getHigh(root.right)) + 1;
    }

}
